package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.cave.CaveMain;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class CaveBaseData {
    private static Logger logger = LoggerFactory.getLogger(CaveBaseData.class);
    private static ConcurrentHashMap<Integer, CaveMain> caveMainMap = new ConcurrentHashMap<>(); // 怪兽洞窟配置表

    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initCaveMain());
    }


    /**
     * 初始化怪兽洞窟配置表
     */
    public static Future<Void> initCaveMain() {
        String table = "cave_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, CaveMain> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        CaveMain entity = new CaveMain(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    caveMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static CaveMain getCaveMain(int id) {
        CaveMain result = caveMainMap.get(id);
        if (result == null) {
            logger.error("getCaveMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, CaveMain> getCaveMainMap() { return caveMainMap; }

    // 获取这个区域的最底层，floor_id最小0，滞留层 floor_type=1
    public static CaveMain getBottomFloorByAreaType(int areaType) {
        CaveMain result = null;
        for (CaveMain caveMain : getCaveMainMap().values()) {
            if (caveMain.getAreaType() == areaType && caveMain.isBottomFloor()) {
                result = caveMain;
            }
        }
        return result;
    }

    // 获取这个区域的最顶层，floor_id最大，
    public static CaveMain getTopFloorByAreaType(int areaType) {
        int floorId = 0;
        CaveMain result = null;
        for (CaveMain caveMain : getCaveMainMap().values()) {
            if (caveMain.getAreaType() == areaType && caveMain.getFloorId() > floorId) {
                result = caveMain;
                floorId = caveMain.getFloorId();
            }
        }
        return result;
    }

    // 获取该区域的某个范围的层列表 floor_id 越大表示越顶层
    public static JsonArray getCaveMainIdList(int areaType, int curFloorId, int num) {
        JsonArray jsonArray = new JsonArray();
        CaveMain caveMainTop = getTopFloorByAreaType(areaType);
        int maxFloorId = curFloorId + num;
        maxFloorId = Math.min(maxFloorId, caveMainTop.getFloorId());// 不能超过本区域的最高层
        for (CaveMain caveMain : getCaveMainMap().values()) {
            if (caveMain.getAreaType() == areaType && caveMain.getFloorId() <= maxFloorId && caveMain.getFloorId() > curFloorId) {
                jsonArray.add(caveMain.getId());
            }
        }
        return jsonArray;
    }

    // 获取该区域底部非滞留层的最后5个坑位
    public static JsonArray getBottomList(int areaType) {
        JsonArray jsonArray = new JsonArray();
        CaveMain caveMainBottom = getBottomFloorByAreaType(areaType);
        int maxFloorId = caveMainBottom.getFloorId() + AllParam.CAVE_LOCATION_MAX[1];// 滞留层=1，则取2,3,4,5,6这5层
        for (CaveMain caveMain : getCaveMainMap().values()) {
            if (caveMain.getAreaType() == areaType && !caveMain.isBottomFloor() && caveMain.getFloorId() <= maxFloorId) {
                jsonArray.add(caveMain.getId());
            }
        }
        return jsonArray;
    }

    // 根据区域，层获取cavemain
    public static CaveMain getCaveMain(int areaType, int floorId) {
        for (CaveMain caveMain : getCaveMainMap().values()) {
            if (caveMain.getAreaType() == areaType && caveMain.getFloorId() == floorId) {
                return caveMain;
            }
        }
        return null;
    }
}
